#include <stdio.h>
#include <stdlib.h>

// 定义树节点结构
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};

// 创建新节点
struct TreeNode* new_node(int val) {
    struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));
    node->val = val;
    node->left = NULL;
    node->right = NULL;
    return node;
}

// 插入节点
struct TreeNode* insert_into_bst(struct TreeNode* root, int val) {
    if (root == NULL) {
        return new_node(val);
    }
    if (val < root->val) {
        root->left = insert_into_bst(root->left, val);
    } else {
        root->right = insert_into_bst(root->right, val);
    }
    return root;
}

// 辅助函数：打印树（中序遍历）
void inorder(struct TreeNode* root) {
    if (root != NULL) {
        inorder(root->left);
        printf("%d ", root->val);
        inorder(root->right);
    }
}

int main() {
    struct TreeNode* root = NULL;
    root = insert_into_bst(root, 5);
    insert_into_bst(root, 3);
    insert_into_bst(root, 7);
    insert_into_bst(root, 2);
    insert_into_bst(root, 4);
    insert_into_bst(root, 6);
    insert_into_bst(root, 8);
    printf("Inorder traversal of the BST: ");
    inorder(root);
    printf("\n");
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>

struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};

bool is_valid_bst_util(struct TreeNode* node, long long min, long long max) {
    if (node == NULL) {
        return true;
    }
    if (node->val <= min || node->val >= max) {
        return false;
    }
    return is_valid_bst_util(node->left, min, node->val) && is_valid_bst_util(node->right, node->val, max);
}

bool is_valid_bst(struct TreeNode* root) {
    return is_valid_bst_util(root, LLONG_MIN, LLONG_MAX);
}

int main() {
    struct TreeNode* root = new_node(5);
    root->left = new_node(3);
    root->right = new_node(7);
    root->left->left = new_node(2);
    root->left->right = new_node(4);
    root->right->left = new_node(6);
    root->right->right = new_node(8);

    if (is_valid_bst(root)) {
        printf("The binary tree is a valid BST.\n");
    } else {
        printf("The binary tree is not a valid BST.\n");
    }
    return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>

struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};

bool is_valid_bst_util(struct TreeNode* node, long long min, long long max) {
    if (node == NULL) {
        return true;
    }
    if (node->val <= min || node->val >= max) {
        return false;
    }
    return is_valid_bst_util(node->left, min, node->val) && is_valid_bst_util(node->right, node->val, max);
}

bool is_valid_bst(struct TreeNode* root) {
    return is_valid_bst_util(root, LLONG_MIN, LLONG_MAX);
}

int main() {
    struct TreeNode* root = new_node(5);
    root->left = new_node(3);
    root->right = new_node(7);
    root->left->left = new_node(2);
    root->left->right = new_node(4);
    root->right->left = new_node(6);
    root->right->right = new_node(8);

    if (is_valid_bst(root)) {
        printf("The binary tree is a valid BST.\n");
    } else {
        printf("The binary tree is not a valid BST.\n");
    }
    return 0;
}
